#include "fs.h"

void start_fs(FILE* hdd){
	//command & args vars declaration
	commandline = (char*) malloc (300);
	command = (char*) malloc (100);
	arg0 = (char*) malloc (100);
	arg1 = (char*) malloc (100);
	
	//filesystem main loop
	do{
		//reinitialization command & args
		strcpy(command,"");
		strcpy(arg0,"");
		strcpy(arg1,"");
		
		//get user input
		commandline = recvMessage();
		printf("command received in fs = %s\n",commandline);
		
		//tokenize user input
		token = strtok(commandline, " ");
		int i = 0;
		while (token) {
			switch (i){
				case 0: strcpy(command,token); break;
				case 1: strcpy(arg0,token); break;
				case 2: strcpy(arg1,token); break;
			}
			token = strtok(NULL, " ");
			i++;
		}
		
		//proses message
		if(strcmp(command,"mountfs")==0){
			//hilangkan @ dari filepath	
			mountedfile = ilanginAt(arg0);
			mountpoint = (char*) malloc(100);
			strcpy(mountpoint,arg1);
			//inisiasi list of dir inode number
			for(i = 0; i < 500; i++){
				cwd[i] = 0;
			}
			//ambil inode number dir root, masukin ke cwd[0] (nunggu inode beres dulu)
			cwd[0] = 1;
		} else if(strcmp(command,"pwd")==0){
			//cwd adalah list of inode number dari current directory
			i = 0;
			//untuk semua unsur list of inode number, tulis nama direktorinya dipisahkan dengan slash '/'
			do{
				printf("%s/",getInodeName(cwd[i],hdd));
			}while(cwd[i]!=0);
			//print new line
			printf("\n");
		} else if(strcmp(command,"ls")==0){
			if(arg0==""){
				//bila tidak ada argumen, maka tulis file2 di direktori current
				dir_t currentdir = getDirFromPath(hdd,cwd);
				int j = 0;
				while(currentdir.direlements[j].inode_no!=0){
					printf("%s\n",currentdir.direlements[j].name);
					j++;
				}
			} else {
				//ada argumennya berupa path yang akan di cari tahu file2 di direktori tersebut
				//tokenize path berdasarkan slash
				char* dirpath[500];
				dirpath[0] = (char*) malloc (17);
				dirpath[0] = "";
				char * temptoken;
				temptoken = strtok(ilanginMountPoint(mountpoint,arg0), "/");
				int j = 0;
				while (temptoken) {
					if(j>0){
						dirpath[j] = (char*) malloc (17);
						strcpy(dirpath[j],temptoken);
					}
					token = strtok(NULL, "/");
					j++;
				}
				//hasil tokenize path kemudian ditelusuri untuk didapat dir nya
				dir_t dir;
				bool found = false;
				readDir(&dir,hdd,0);
				j = 1;
				while(dirpath[j]!=""){
					int k = 0;
					while(dir.direlements[k].inode_no!=0 && strcmp(dir.direlements[k].name,dirpath[j])!=0){
						k++;
					}
					//tidak ada penanganan bila direktori tidak ada
					readDir(&dir,hdd,dir.direlements[k].inode_no);
					j++;
				}
				
				j = 0;
				while(dir.direlements[j].inode_no!=0){
					printf("%s\n",dir.direlements[j].name);
					j++;
				}
			}
		} else if(strcmp(command,"mkdir")==0){
			//ambil current directory
			dir_t currentdir = getDirFromPath(hdd,cwd);
			//buat inode dan dapetin inode no nya
			unsigned inode_no = createInodeDir(hdd,arg0);
			//daftarkan di direktori
			insert(&currentdir, inode_no,arg0);
		} else if(strcmp(command,"cat")==0){
			//ada argumennya berupa path yang akan di cari tahu file2 di direktori tersebut
			//tokenize path berdasarkan slash
			char* dirpath[500];
			dirpath[0] = (char*) malloc (17);
			dirpath[0] = "";
			char * temptoken;
			temptoken = strtok(ilanginMountPoint(mountpoint,arg0), "/");
			int j = 0;
			while (temptoken) {
				if(j>0){
					dirpath[j] = (char*) malloc (17);
					strcpy(dirpath[j],temptoken);
				}
				token = strtok(NULL, "/");
				j++;
			}
			//hasil tokenize path kemudian ditelusuri untuk didapat dir nya
			dir_t dir;
			bool found = false;
			readDir(&dir,hdd,0);
			j = 1;
			while(dirpath[j]!=""){
				int k = 0;
				while(dir.direlements[k].inode_no!=0 && strcmp(dir.direlements[k].name,dirpath[j])!=0){
					k++;
				}
				//tidak ada penanganan bila direktori tidak ada
				if(getInodeType(dir.direlements[k].inode_no,hdd)=='2'){
					readDir(&dir,hdd,dir.direlements[k].inode_no);
				} else if(getInodeType(dir.direlements[k].inode_no,hdd)=='1'){
					tAddrBlock* addrBlocks = getInodeAddr(dir.direlements[k].inode_no,hdd);
					int m = 0;
					while(addrBlocks[m].addr!='\0'){
						int n;
						block_t block = readBlock(hdd,addrBlocks[m].addr);
						for(n = 0; n < 4000; n++){
							printf("%c",block.bytes[n]);
						}
						m++;
					}					
				}
				j++;
			}
			
			j = 0;
			while(dir.direlements[j].inode_no!=0){
				printf("%s\n",dir.direlements[j].name);
				j++;
			}
		} else {
		}
		
		//kembalikan feedback message
	}while(strcmp(command,"exit")!=0);
}
